﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using Timed;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using LuaInterface;
using Iso;

namespace MYSPlugin
{
    public static class CustomLua
    {
        public static void Register()
        {
            Engine.lua.RegisterFunction("OverText", null, typeof(CustomLua).GetMethod("OverText"));
            Engine.lua.RegisterFunction("AddDialogManager", null, typeof(CustomLua).GetMethod("AddDialogManager"));
            Engine.lua.RegisterFunction("Say", null, typeof(CustomLua).GetMethod("Say"));
            Engine.lua.RegisterFunction("TimedSay", null, typeof(CustomLua).GetMethod("TimedSay"));
            Engine.lua.RegisterFunction("SetObjectTextureMatrix", null, typeof(CustomLua).GetMethod("SetObjectTextureMatrix"));
            Engine.lua.RegisterFunction("GetCenterCoord", null, typeof(CustomLua).GetMethod("GetCenterCoord"));
            Engine.lua.RegisterFunction("GetQuadCoordX", null, typeof(CustomLua).GetMethod("GetQuadCoordX"));
            Engine.lua.RegisterFunction("GetQuadCoordY", null, typeof(CustomLua).GetMethod("GetQuadCoordY"));
            Engine.lua.RegisterFunction("GridDistance", null, typeof(CustomLua).GetMethod("GridDistance"));
            Engine.lua.RegisterFunction("GetMouseIntersect", null, typeof(CustomLua).GetMethod("GetMouseIntersect"));
            Engine.lua.RegisterFunction("SumVector3", null, typeof(CustomLua).GetMethod("SumVector3"));
            Engine.lua.RegisterFunction("PlayVideo", null, typeof(CustomLua).GetMethod("PlayVideo"));
        }

        public static void OverText(string value, Vector3Df position)
        {
            for (int i = 0; i < 1024; i++)
            {
                if (!((Scene)Engine.currentScene).plugins.ContainsKey("OT" + i))
                {
                    ((Scene)Engine.currentScene).plugins.Add("OT" + i, new OverText("OT" + i, value, position));
                    ((Scene)Engine.currentScene).plugins["OT" + i].Load();
                    break;
                }
            }
        }

        public static void AddDialogManager()
        {
            ((Scene)Engine.currentScene).plugins.Add("dialogManager", new DialogManager());
        }

        public static void Say(string title, string icon, string text, string onClose)
        {
            ((DialogManager)(((Scene)Engine.currentScene).plugins["dialogManager"])).Add(title, Engine.device.VideoDriver.GetTexture("data/ui/textIcons/" + icon + ".png"), text, onClose);
        }

        public static void TimedSay(string title, string icon, string text, uint timecode, string onClose)
        {
            Engine.timeline.operations.Add(new TimedOperation(timecode,
                new EventHandler<TimedEventArgs>((object s, TimedEventArgs args) =>
                { ((DialogManager)(((Scene)Engine.currentScene).plugins["dialogManager"])).Add(title, Engine.device.VideoDriver.GetTexture("data/ui/textIcons/" + icon + ".png"), text, onClose); })));
        }

        public static void SetObjectTextureMatrix(string name, float rotation, float rpX, float rpY, float tX, float tY, float sX, float sY)
        {
            ((Scene)Engine.currentScene).objects[name].sceneNode.GetMaterial(0).SetTextureMatrix(0, Matrix.Identity.BuildTextureTransform(
                rotation, new Vector2Df(rpX, rpY), new Vector2Df(tX, tY), new Vector2Df(sX, sY)));
        }

        public static Vector3Df GetCenterCoord(int X, int Y, float size, float oy)
        {
            return new Vector3Df(X * size, oy, Y * size);
        }

        public static int GetQuadCoordX(Vector3Df pos, float size)
        {
            return (int)Math.Round((pos.X) / size, 0);
        }

        public static int GetQuadCoordY(Vector3Df pos, float size)
        {
            return (int)Math.Round((pos.Z) / size, 0);
        }

        public static float GridDistance(int x1, int y1, int x2, int y2)
        {
            return (float)Math.Sqrt(Math.Abs(Math.Pow(y2 - y1, 2) + Math.Pow(x2 - x1, 2)));
        }

        public static Vector3Df GetMouseIntersect()
        {
            Vector3Df collisionPoint;
            Triangle3Df collisionTriangle;
            SceneNode collisionNode;
            Engine.device.SceneManager.SceneCollisionManager.GetCollisionPoint(
                Engine.device.SceneManager.SceneCollisionManager.GetRayFromScreenCoordinates(LuaHook.GetMousePos()),
                ((Scene)Engine.currentScene).sel, out collisionPoint, out collisionTriangle, out collisionNode);
            return collisionPoint;
        }

        public static Vector3Df SumVector3(Vector3Df a, Vector3Df b)
        {
            return a + b;
        }

        public static void PlayVideo(string filename, string onEnd)
        {
            Engine.LoadScene(new VideoPlayer("data\\fmv\\" + filename, onEnd));
        }
    }
}
